package cn.edu.ecnu.gis.client.view;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class GoogleMap {

    private static int nextId = 0;
    private JavaScriptObject map;

    public GoogleMap(Panel panel) {
//        String id = "map" + (++nextId);
//        setStyleName("gwtapps-GoogleMap");
//        setHTML("<div id='" + id + "' style='height:100%;width:100%'></div>");
//        RootPanel.get().add(this);
//        //panel.add(this);
//        map = initMap(id);
    }
    
    public void addMarker(JSONArray point) {
        addMarker(getMap(),point);

    }

    public void addGeoXML(String geoXmlAddress) {
        addGeoXML(getMap(),geoXmlAddress);
    }

    public void addControl() {
        addControl(getMap());
    }

    private native static void addControl(JavaScriptObject map)/*-{
    var largeMapControl =new $wnd.GLargeMapControl();
    map.addControl(largeMapControl);
    }-*/;

    private native static void addGeoXML(JavaScriptObject map, String geoXmlAddress)/*-{
    var geoXml=new $wnd.GGeoXml(geoXmlAddress);
    map.addOverlay(geoXml);
    geoXml.show();
    }-*/;
//[instance-expr.]@class-name::method-name(param-signature)(arguments)

    private native static void addMarker(JavaScriptObject map, JSONArray point)/*-{
    //map.clearOverLays();
    var lat = point.@com.google.gwt.json.client.JSONArray::get(I)(0);
    var lon=point.@com.google.gwt.json.client.JSONArray::get(I)(1);
    var point1=new $wnd.GLatLng(lat/100000,lon/100000);
    //var point1=new $wnd.LTPoint(lat/100000,lon/100000);
    map.setCenter(point1,13);
    map.addOverlay(new $wnd.GMarker(point1));
    var mapControl = new $wnd.GMapTypeControl();
    var mapSmallContorl =new $wnd.GSmallMapControl();
    map.addControl(mapControl);
    map.addControl(mapSmallContorl);
    map.openInfoWindow(map.getCenter(),$document.createTextNode(lat, lon));
    }-*/;

    public void setCenter(final double lat, final double lon) {
        setCenterImpl(getMap(),lat, lon);
    }

    private native static JavaScriptObject initMap(String id)/*-{
    var map = new $wnd.GMap2( $doc.getElementById(id) );
    return map;
    }-*/;

    private native static void setCenterImpl(JavaScriptObject map, double lat, double lon)/*-{
    map.setCenter(new $wnd.GLatLng(lat, lon), 11);
    }-*/;

    public JavaScriptObject getMap() {
        return map;
    }

    public void setMap(JavaScriptObject map) {
        this.map = map;
    }
}
